import { Controller, Get, Post, Put, Delete, Param, Body, UseGuards, Req } from '@nestjs/common';
import { CategoryService } from './category.service';
import { Category } from './category.entity';
import { JwtHttpGuard } from '../auth/jwt-http.guard';

@Controller('api/category')
@UseGuards(JwtHttpGuard)
export class CategoryController {
  constructor(private readonly categoryService: CategoryService) {}

  @Get('list')
  async findAll(): Promise<Category[]> {
    return await this.categoryService.findAll();
  }

  @Get('tree')
  async getTree(): Promise<Category[]> {
    return await this.categoryService.getTree();
  }

  @Get('authorized')
  async getAuthorizedCategories(@Req() request): Promise<Category[]> {
    const user = request.user;
    return await this.categoryService.getAuthorizedCategories(user);
  }

  @Get(':id')
  async findOne(@Param('id') id: string): Promise<Category | null> {
    return await this.categoryService.findOne(+id);
  }

  @Post()
  async create(@Body() categoryData: Partial<Category>): Promise<Category> {
    return await this.categoryService.create(categoryData);
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() categoryData: Partial<Category>): Promise<Category | null> {
    return await this.categoryService.update(+id, categoryData);
  }

  @Delete(':id')
  async remove(@Param('id') id: string): Promise<void> {
    return await this.categoryService.remove(+id);
  }
}